package com.example.analysis.service.impl;

import com.example.analysis.entity.Course;
import com.example.analysis.mapper.CourseDao;
import com.example.analysis.service.CourseService;
import com.example.analysis.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CourseServiceImpl implements CourseService {
    @Resource
    private CourseDao CourseDao;

    @Autowired
    private CourseDao courseDao;

    @Override
    public Response<List<Course>> getAllCourses() {
        List<Course> courses = courseDao.getAllCourses();
        if (!courses.isEmpty()) {
            return new Response<>(true, "Success", courses);
        } else {
            return new Response<>(false, "No courses found", null);
        }
    }
    @Override
    public Response<Course> update(Course course) {
        try {
            courseDao.update(course);
            return new Response<>(true, "Update success", course);
        } catch (Exception e) {
            return new Response<>(false, "Update failed: " + e.getMessage(), null);
        }
    }
    @Override
    public Response<Course> deleteByCourseId(String courseId) {
        try {
            Course course = courseDao.selectByCourseId(courseId);
            if (course != null) {
                courseDao.deleteByCourseId(courseId);
                return new Response<>(true, "Delete success", course);
            } else {
                return new Response<>(false, "Course not found", null);
            }
        } catch (Exception e) {
            return new Response<>(false, "Delete failed: " + e.getMessage(), null);
        }
    }
    @Override
    public Response<Course> insert(Course aCourse) {
        try {
            CourseDao.insert(aCourse);
            return new Response<>(true, "Insert success", null);
        } catch (Exception e) {
            return new Response<>(false, "Insert failed: " + e.getMessage(), null);
        }
    }

    @Override
    public Response<Course> getById(String CourseId) {
        Course aCourse = CourseDao.selectByPrimaryKey(CourseId);
        if (aCourse != null) {
            return new Response<>(true, "Success", aCourse);
        } else {
            return new Response<>(false, "Administration not found", null);
        }
    }

    @Override
    public Response<Course> getByName(String CourseName) {
        Course aCourse = CourseDao.selectByName(CourseName);
        if (aCourse != null) {
            return new Response<>(true, "Success", aCourse);
        } else {
            return new Response<>(false, "Administration not found", null);
        }
    }

    @Override
    public Response<List<Course>> getAll() {
        List<Course> courseList = courseDao.getAll();

        if (!courseList.isEmpty()) {
            return new Response<>(true, "Success", courseList);
        } else {
            return new Response<>(false, "Administration not found", null);
        }
    }

    @Override
    public Response<List<Course>> getCourseData(String classId) {
        List<Course> courseList = courseDao.getCourseData(classId);

        if (!courseList.isEmpty()) {
            return new Response<>(true, "Success", courseList);
        } else {
            return new Response<>(false, "Administration not found", null);
        }
    }
}
